/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.application.NWConstants;
import com.jb.web.bean.Form;
import com.jb.web.bean.FormField;
import com.jb.web.bean.TableRow;
import com.jb.web.context.WebContext;

import java.util.List;

import com.jb.faces.component.UIComponent;


public class TableFormRenderer extends TableRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        Form form = (Form) itemToRender;

        StringBuffer html = new StringBuffer("");

        if (!isVisible(pCtx, form)) {
            return html;
        }

        List list = form.getDescendants(FormField.class);
        String action = (String) evaluate(pCtx, form, form.getAction());
        action = pCtx.formURL(action, form);

        html.append("<FORM ").append(addTagProperty("id", "f" + form.getClientId(), null))
            .append(addTagProperty("name", "f" + form.getClientId(), null))
            .append(addTagProperty("enctype", form.getEncType(), null))
            .append(addTagProperty("enctype", form.getEncType(), null)).append(addTagProperty("method",
                form.getMethod(), "post"));

        html.append(addTagProperty("action", action, ""));

        html.append("><TABLE ").append(addTagProperty("cellSpacing", "" + form.getGridLineWidth(), null))
            .append(addTagProperty("cellPadding", "" + form.getCellPadding(), null))
            .append(addTagProperty("style", form.getStyle(pCtx), null))
            .append(addTagProperty("class", form.getStyleClass(), null))
            .append(addTagProperty("width", form.getWidth(), null)).append(getEditScript(pCtx, form)).append(">");

        if (form.isShowHeader() && (form.getHeader(pCtx) != null)) {
            html.append("<thead> ").append(form.getHeader(pCtx).render(pCtx)).append("</thead>");
        }

        list = form.getChildrenByType(FormField.class);

        for (int i = 0; i < list.size(); i++) {
            FormField hi = (FormField) list.get(i);
            html.append(hi.render(pCtx));
        }

        for (int i = 0; i < form.getRows(); i++) {
            TableRow row = form.getRow(i);

            if (row != null) {
                html.append(row.render(pCtx));
            }
        }

        html.append("<input type=\"hidden\" name=\"" + NWConstants.NW_EVENT_SOURCE + "\" value=\"")
            .append(form.getQName()).append("\" />");

        if (form.isShowFooter() && (form.getFooter(pCtx) != null)) {
            html.append(form.getFooter(pCtx).render(pCtx));
        }

        html.append("</Table>").append("</FORM>");

        /*
                if ("multipart/form-data".equals(form.getEncType())) {
                    html.append(
                        "<iframe style=\"width:0px; height:0px; border: 0px\" id=\"_hiddenIFrame" +
                        form.getClientId() + "\" name=\"_hiddenIFrame" +
                        form.getClientId() + "\" ></iframe> ");
                }
        */
        return html;
    }
}
